//
//  String.swift
//  CMKit
//
//  Created by travel on 2016/12/15.
//  Copyright © 2016年 covermedia. All rights reserved.
//

import Foundation

public extension String {
    
    //    /// 转化为 md5值
    //    var md5: String {
    //        let str = self.cString(using: String.Encoding.utf8)
    //        let strLen = CC_LONG(self.lengthOfBytes(using: String.Encoding.utf8))
    //        let digestLen = Int(CC_MD5_DIGEST_LENGTH)
    //        let result = UnsafeMutablePointer<CUnsignedChar>.allocate(capacity: digestLen)
    //
    //        CC_MD5(str!, strLen, result)
    //
    //        let hash = NSMutableString()
    //        for i in 0..<digestLen {
    //            hash.appendFormat("%02x", result[i])
    //        }
    //
    //        result.deallocate(capacity: digestLen)
    //
    //        return String(format: hash as String)
    //    }
    
    
    /// 移除首尾空格
    ///
    /// - Returns: 移除首尾空格后的字符串
    public func trim()-> String {
        return self.trimmingCharacters(in: CharacterSet.whitespaces)
    }
    
    /// string 转化为 URL
    public var toURL: URL? {
        if let u = URL.init(string: self) {
            return u
        } else if let du = self.addingPercentEncoding(withAllowedCharacters: CharacterSet.urlQueryAllowed), let u1 = URL.init(string: du) {
            return u1
        }
        return nil
    }
}
